﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-27>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：来涵采灌计划调整信息
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Web.Controls.LAIHAN;

namespace Web.Controls
{
    public partial class UCLHCaiGuanJHQS : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX
        {
            get;
            set;
        }

        /// <summary>
        /// 当前流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC
        {
            get;
            set;
        }

        /// <summary>
        /// 来函编号
        /// </summary>
        public string LAIHANBH
        {
            set
            {
                this.txtLaiHanBH.Text = value;
            }
            get
            {
                return this.txtLaiHanBH.Text;
            }
        }



        /// <summary>
        /// 判断引用页面类别   1月调整登记  2月调整修改  3其他页面调用 不可修改
        /// </summary>
        public int ControlType
        {
            get { return Convert.ToInt32(ViewState["ControlType"]); }
            set { ViewState["ControlType"] = value; }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        private string errorMsg
        {
            get
            {
                return Convert.ToString(ViewState["errorMsg"]);
            }
            set
            {
                ViewState["errorMsg"] = value;
            }
        }

        #endregion

        #region 变量

        #endregion

        #region 事件

        /// <summary>
        /// 加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "bh")//响应用户号回车事件
            {
                DataRowCollection drc = new SpHelper().P_QS_JIHUAXXHQ(null,(int)this.LaiHanLX,this.txtYongHuH.Text,Convert.ToInt32(this.cmbY.Value),BusinessTool.WType).Tables[0].Rows;
                if (drc.Count == 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = false;
                    cr.Msg = "查询不到该用户计划信息。";
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
                else
                {
                    cr.AddValue(Convert.ToString(drc[0]["S_YONGHUM"]), Convert.ToString(drc[0]["I_KAICAIJHYUAN"]), Convert.ToString(drc[0]["I_HUIGUANJHYUAN"]));
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {
            YS_LH_LAIHANXX laihanxx = new YS_LH_LAIHANXX();
            laihanxx.S_LAIHANBH = null;
            laihanxx.I_LAIHANLX = (int)this.LaiHanLX;
            laihanxx.I_LIUCHENGBH = (int)eLaiHanLCBH.未登记;
            laihanxx.S_YONGHUBH = this.txtYongHuH.Text;
            laihanxx.S_BEIZHU = this.txtYongHuH.Text;
            //this.LoadInfo(laihanxx);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            this.txtYongHuH.ClientEnabled = true;
            this.txtXinJH.ClientEnabled = true;
            this.txtXinJHHG.ClientEnabled = true;
            this.cmbY.ClientEnabled = true;
            this.txtLaiHanBH.ClientEnabled = false;
            this.txtXuKeZH.ClientEnabled = false;
            this.txtXianJH.ClientEnabled = false;
            this.txtXianJHHG.ClientEnabled = false;
            this.txtYongHuM.ClientEnabled = false;
            int y=BusinessTool.GetSLYMorZWNY(1,BusinessTool.WType)/100;
            this.cmbY.Items.Add(y.ToString());
            this.cmbY.SelectedIndex = 0;
            //this.cmbY.Items.Add(y.ToString());
            this.txtYongHuH.Focus();
        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //根据类型显示需要的             
            //受理之后不可编辑.下方switch中case 3可去除.使用此控件的受理之后的页面该控件属性需加上LaiHanLC="受理"
            if ((int)this.LaiHanLC < (int)eLaiHanLCBH.预登记)
            {
                this.txtYongHuH.ClientEnabled = true;
                this.txtXinJH.ClientEnabled = true;
                this.txtXinJHHG.ClientEnabled = true;
                this.cmbY.ClientEnabled = true;
            }
            if ((int)this.LaiHanLC >= (int)eLaiHanLCBH.预登记)
            {
                this.txtYongHuH.ClientEnabled = false;
                this.txtXinJH.ClientEnabled = false;
                this.txtXinJHHG.ClientEnabled = false;
                this.cmbY.ClientEnabled = false;
            }
        }

        /// <summary>
        /// 验证数据
        /// </summary>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            //判断用户号是否存在
            if (this.txtYongHuH.Text == "")
            {
                rs.Msg = "请输入用户号。";
                return rs;
            }
            if (this.txtXianJH.Text == "")
            {
                rs.Msg = "该用户无对应计划信息。";
                return rs;
            }
            if (this.txtXinJH.Text == "")
            {
                rs.Msg = "请输入新开采计划。";
                return rs;
            }
            if (!PageTools.ValidateString(this.txtXinJH.Text,PageTools.eValidateType.数字))
            {
                rs.Msg = "请输入正确的新开采计划。";
                return rs;
            }
            if (this.txtXinJHHG.Text == "")
            {
                rs.Msg = "请输入新回灌计划。";
                return rs;
            }
            if (!PageTools.ValidateString(this.txtXinJHHG.Text, PageTools.eValidateType.数字))
            {
                rs.Msg = "请输入正确的新回灌计划。";
                return rs;
            }
            if (this.txtXinJH.Text == this.txtXianJH.Text&&this.txtXinJHHG.Text==this.txtXianJHHG.Text)
            {
                rs.Msg = "开采计划与回灌计划均未变更,请变更后再提交。";
                return rs;
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearInfo()
        {
            this.txtYongHuH.Text = "";
            this.txtYongHuM.Text = "";
            this.txtLaiHanBH.Text = "";
            this.txtXuKeZH.Text = "";            
            this.txtXianJH.Text = "";
            this.txtXinJH.Text = "";
            this.txtXianJHHG.Text = "";
            this.txtXinJHHG.Text = "";
        }


        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            //登记前
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                SetDisplay();
                return;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                SetDisplay();
            }
            //加载许可号等信息
            this.LAIHANBH = laihanxx.S_LAIHANBH;
            this.txtXuKeZH.Text = laihanxx.S_XUKEBH;
            //加载信息
            DataRow dr = new SpHelper().P_QS_JIHUAXXHQ(laihanxx.S_LAIHANBH, (int)this.LaiHanLX,laihanxx.S_YONGHUBH,null,BusinessTool.WType).Tables[0].Rows[0];
            this.txtYongHuH.Text = laihanxx.S_YONGHUBH;
            this.txtLaiHanBH.Text = laihanxx.S_LAIHANBH;
            this.cmbY.Value = laihanxx.I_Y.ToString();
            this.txtYongHuM.Text = Convert.ToString(dr["S_YONGHUM"]);
            this.txtXianJH.Text = Convert.ToString(dr["I_KAICAIJHYUAN"]);
            this.txtXianJHHG.Text = Convert.ToString(dr["I_HUIGUANJHYUAN"]);
            this.txtXinJH.Value = Convert.ToString(dr["I_KAICAIJHXIN"]);
            this.txtXinJHHG.Value = Convert.ToString(dr["I_HUIGUANJHXIN"]);
        }

        /// <summary>
        /// 获取来函主表信息
        /// </summary>
        /// <param name="laihanid">ID</param>
        /// <param name="liuchengzt">0:收件 1:收件并登记</param>
        /// <param name="msg">输出消息,正常为空</param>
        /// <returns>null:验证失败 !null:成功返回</returns>
        public YS_LH_LAIHANXX GetLAIHANXX(int laihanid, int liuchengzt, out string msg)
        {
            YS_LH_LAIHANXX laihan = new YS_LH_LAIHANXX();
            string laihanbh = "";
            string xukebh = "";
            msg = "";
            laihan.ID = laihanid;
            if (laihanid == 0)
            {
                
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(this.txtYongHuH.Text, out zd, out glk, out ywy);
                laihan.S_ZHANDIANBH = zd;
                laihan.S_GUANLIKBH = glk;
                laihan.I_YWY = int.Parse(ywy);
                laihan.S_YWY = BusinessTool.GetYWYName(ywy);
                //laihan.S_YHYWY = ywy;
                //DataSet numset = new SpHelper().P_YS_LH_XUKEBH(glk, 1, 0, Convert.ToString(this.cmbY.Value), out xukebh, out laihanbh);
                new SpHelper().P_ALL_LH_BHSC(BusinessTool.WType, (int)this.LaiHanLX, BusinessTool.GetWaterYM() / 100, out laihanbh);
                laihan.I_XT = BusinessTool.WType;
                laihan.D_SHURURQ = DateTime.Now;
                laihan.D_LAIHANRQ = DateTime.Now;
                laihan.I_JLZT = 0;
                laihan.I_LAIHANLX = (int)this.LaiHanLX;
                laihan.I_SHURUR = LoginInfo.CurrentUser.UserID;
                laihan.I_AUTOTYPE = 0;
                laihan.S_LAIHANBH = laihanbh;
                laihan.S_BEIYONG1 = xukebh;
                laihan.S_XUKEBH = xukebh;
                laihan.I_LIUCHENGZT = (int)eLaiHanLCZT.流程中;
                //laihan.I_Y = BusinessTool.GetWaterYM() / 100;
                laihan.I_TZY = laihan.I_Y = Convert.ToInt32(this.cmbY.Value);
                //laihan.I_M = BusinessTool.GetWaterYM() % 100;
                laihan.S_BEIZHU = this.txtYongHuH.Text;
                laihan.S_YONGHUBH = this.txtYongHuH.Text;
                laihan.S_YONGHUM = this.txtYongHuM.Text;
            }
            else
            {
                laihan = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(laihan.ID);
            }
            laihan.I_LIUCHENGBH = liuchengzt;
            laihan.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            laihan.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return laihan;
        }

        /// <summary>
        /// 获取来函用户信息
        /// </summary>
        /// <param name="laihan"></param>
        /// <returns></returns>
        public YS_LH_YONGHUXX GetLHYONGHUXX(YS_LH_LAIHANXX laihan)
        {
            return null;
        }

        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            if (this.LaiHanLX == eLaiHanLX.水资源采灌计划)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_CAIGUANJHXXProcess bllT2 = new QS_LH_CAIGUANJHXXProcess();
                bllT2.DeleteQS_LH_CAIGUANJHXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                QS_LH_CAIGUANJHXX item = new QS_LH_CAIGUANJHXX();
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                item.I_Y = laihanxx.I_Y;
                item.S_YONGHUH = laihanxx.S_YONGHUBH;
                item.I_KAICAIJHXIN = int.Parse(this.txtXinJH.Text);
                item.I_KAICAIJHYUAN = int.Parse(this.txtXianJH.Text);
                item.I_HUIGUANJHXIN = int.Parse(this.txtXinJHHG.Text);
                item.I_HUIGUANJHYUAN = int.Parse(this.txtXianJHHG.Text);
                bllT2.InsertOrUpdate(item);
            }
        }

        #endregion

    }
}